package com.luxury;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;

//这个网址显示的都是url写死的 不是通过json串来分析的
public class HomeType1Activity extends AppCompatActivity {
    private static final String TAG = "hometye1Activity";
    private Context mContext = this;
    private String titles = "";
    private String urls = "";
    @BindView(R.id.wv_hometype1Activity)
    WebView wvHometype1Activity;
    @BindView(R.id.image_return)
    ImageView imageReturn;
    @BindView(R.id.image_share)
    ImageView imageShare;
    @BindView(R.id.text_webactivity)
    TextView textWebactivity;
    private List<String> list_itemType1Urls = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ShareSDK.initSDK(this);//初始化ShareSDK

        setContentView(R.layout.activity_hometype1);
        ButterKnife.bind(this);
        String url = getIntent().getStringExtra("url");
        list_itemType1Urls.add("http://android.secoo.com/appActivity/ne" +
                " wPro" +
                ".shtml?pageid=1054&title=上新");
        list_itemType1Urls.add("http://android.secoo.com/staticpages/hwzy" +
                ".html?pageid=1012&title=海外");
        list_itemType1Urls.add("http://android.secoo.com/appActivity/flash_purchase_list.shtml?pageid=1010&title=SALE");
        list_itemType1Urls.add("http://android.secoo.com/appActivity/app_auction_index" +
                ".shtml?pageid=1011&title=拍卖");
        list_itemType1Urls.add("http://android.secoo.com/cooMaster.html?pageid=1089&title=库大师");
        final int tabIndex = getIntent().getIntExtra("tabIndex", 0);
        wvHometype1Activity.loadUrl(list_itemType1Urls.get(tabIndex));
        urls = list_itemType1Urls.get(tabIndex);
        int type = getIntent().getIntExtra("type", 1);
        switch (type) {
            case 1:
                wvHometype1Activity.loadUrl(list_itemType1Urls.get(tabIndex));
                break;
            case 6:
                wvHometype1Activity.loadUrl(url);
        }
        //使用webView打开网页
        wvHometype1Activity.getSettings().setJavaScriptEnabled(true);
        wvHometype1Activity.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {

                geturl(url);
                return true;
            }
        });
        //写入标题栏
        wvHometype1Activity.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
                textWebactivity.setText(title + "");
                titles = title;
            }
        });
        //禁用系统浏览器

        //点击返回
        imageReturn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        imageShare.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //1.创建一键分享对象
                OnekeyShare share = new OnekeyShare();
                share.disableSSOWhenAuthorize();//关闭sso授权
                //设置一键分享的内容
                share.setText(titles + urls + "");
                share.show(mContext);
            }
        });
    }

    private void geturl(String url) {
        //判断字符串类型 拼接网址
        int start = url.indexOf(":");
        String urltype = url.substring(0, start);

        Log.i(TAG, "geturl: ++++++++++++" + start);
        int begin = url.indexOf("?") + 1;
        int end = url.indexOf("&");
        String productId = url.substring(begin, end);
        String urlString = "http://android.secoo.com/appservice/query_goods.action?" + productId;
        Log.i(TAG, "shouldOverrideUrlLoading:--------------------------- " + url);


        if (urltype.equals("http")) {
            Intent intent = new Intent(this, HomeWebActivity.class);
            intent.putExtra("url", url);
            startActivity(intent);
        } else if (urltype.equals("secoo")) {
            Log.i(TAG, "商品详情json地址" + urlString);
            Intent intent = new Intent(this, ProductActivity.class);
            intent.putExtra("url", urlString);
            intent.putExtra("productId", productId);
            startActivity(intent);
        }

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (wvHometype1Activity.canGoBack()) {
                wvHometype1Activity.goBack();//返回网页的上一页
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }
}
